import recom from '@/components/regionComponent.vue'
export default {
  components: {
    recom
  },
  data() {
    return {
      perList: {
        headportrait: '',
        sex: 0, //0、未设置、1、男 2、女
        birthday: '',
        nickname: '',
        provinceid: '', //省份
        cityid: '', //城市
        province: '', //省份
        city: '', //城市
      }, //个人信息
      sexpopup: false, //性别弹框
      sexlist: [{
        name: '男',
        id: 1
      }, {
        name: '女',
        id: 2
      }],
      params: {
        year: true,
        month: true,
        day: true,
        hour: false,
        minute: false,
        second: false
      },
      showdz: false, //车辆所在地
    }
  },
  onShow() {
    this.ServiceOneselfInfomation()
  },
  created() {
    this.ServiceOneselfInfomation()
  },
  methods: {
    //个人信息
    async ServiceOneselfInfomation() {
      try {
        const res = await this.$api.user.ServiceOneselfInfomation({})
        this.perList = res.data
        this.perList.province = res.data.location
      } catch (err) {}
    },
    //跳转详情
    ieClick(type) {
      uni.navigateTo({
        url: '/pages_userModule/incomeExpenditurDetails/incomeExpenditurDetails?ieid=' + type.moneyId
      })
    },
    //选中性别
    sexclicks(type) {
      this.perList.sex = type.id
      this.sexpopup = false
    },
    //是否认证
    arrroveClick(type) {
      if (type) {
        uni.showToast({
          title: '已认证',
          icon: 'none'
        })
      } else {
        uni.navigateTo({
          url: '/pages_commodity/idCarsAuthentication/idCarsAuthentication?typeid=110'
        })
      }
    },
    //车辆所在地
    confirmdz(e) {
      this.perList.province = e.province.label
      this.perList.city = e.city.label
      this.perList.provinceid = e.province.id
      this.perList.cityid = e.city.id
      this.showdz = false
    },
    close() {
      this.showdz = false
    },
    //保存信息
    preserClick() {
      this.UpdateServiceOneselfInfomation()
    },
    //修改个人信息
    async UpdateServiceOneselfInfomation() {
      // let name = this.perList.name.charAt(0)
      // console.log(name)
      // if(name === ' '){
      //   this.$u.toast('用户名不能有非法字符')
      //   return false
      // }
      try {
        const res = await this.$api.user.UpdateServiceOneselfInfomation({
          headportrait: this.perList.headportrait,
          name: this.$u.trim(this.perList.name),
          mobile: this.perList.mobile,
          province: this.perList.provinceid,
          city: this.perList.cityid,
        })
        if (res.code === 200) {
          uni.showToast({
            title: '保存成功',
            icon: 'none'
          })
          this.GetUserInfomation()

        }
      } catch (err) {}
    },
    //地址
    addressClick() {
      uni.navigateTo({
        url: '/pages_userModule/my_address/my_address'
      })
    },
    // 复制到剪切板 h5不可复制
    copyCode(value) {
      uni.setClipboardData({
        data: value,
        success: function() {
          uni.showToast({
            title: '复制成功',
            icon: 'none'
          })
        }
      })
    },
    // 把图片保存到本地 h5不可保存
    saveImgLocal(value) {
      uni.downloadFile({
        url: value,
        success: res => {
          if (res.statusCode === 200) {
            uni.saveImageToPhotosAlbum({
              filePath: res.tempFilePath,
              success: function() {
                uni.showToast({
                  title: '保存成功',
                  icon: 'none'
                })
              },
              fail: function() {
                uni.showToast({
                  title: '保存失败，请稍后重试',
                  icon: 'none'
                })
              }
            })
          }
        }
      })
    },
    //选择图片
    uploadImage() {
      uni.chooseImage({
        count: 1,
        sourceType: ['album', 'camera'],
        sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
        success: result => {
          uni.showLoading({
            title: '图片上传中~',
          })
          this.$api.files.uploadImg({
            filePath: result.tempFilePaths[0], // 图片流
          }).then(res => {
            console.log(res)
            var urls = res.baseUrl + res.url
            this.perList.headportrait = urls
            console.log(this.perList.headportrait)
            uni.hideLoading()
          })
        },
        error: function(e) {}
      })
    },
    //分享微信好友
    goPartner(type) {
      let code = this.perList.invitecode
      console.log(code)
      uni.share({
        provider: 'weixin',
        scene: type,
        type: 0,
        summary: '在吗？100%纯羊毛等你来薅哟~',
        title: '送1000元购车抵用券+39元免费洗车券！点击领取吧',
        href: 'https://files.yzsheng.com/shareH5/Qmc_Client_H5/index.html?code=' + code, //这地址太长了，就省略了
        imageUrl: 'https://files.yzsheng.com/client/me/img/logo.png',
        success: function(res) {
          console.log('success:' + JSON.stringify(res))
        },
        fail: function(err) {
          console.log('fail:' + JSON.stringify(err))
          uni.showToast({
            title: '分享失败'
          })
        }
      })
    },
  }
}
